package com.seari.web.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seari.commonutils.redis.JedisUtilProxy;
import com.seari.commonutils.redis.ex.JedisNotInitializeException;
import com.seari.main.StartUpClass;
import com.seari.pojo.MessageBean;
import com.seari.service.PersonnelService;

import redis.clients.jedis.Jedis;

@Controller
public class PersonnelController {

	@Autowired
	PersonnelService personnelService;

	@RequestMapping("personnel")
	public String index() {
		return "personnel";
	}

	/**
	 * 车站客流趋势
	 * 
	 * @return
	 */
	@RequestMapping(value = "personnel/flowData")
	@ResponseBody
	public MessageBean getPassFlowData() {
		MessageBean messageBean = new MessageBean();
		Map<String, Object> passFlowData = new HashMap<>();
		try {
			if ("test".equals(StartUpClass.flowData)) {
				String time = "06:00,07:00,08:00,09:00,10:00,11:00,12:00";
				String accessPassFlow = "740,738,644,344,443,541,645";
				String outPassFlow = "639,643,435,438,535,442,347";
				passFlowData.put("time", time);
				passFlowData.put("accessPassFlow", accessPassFlow);
				passFlowData.put("outPassFlow", outPassFlow);
			} else {
				passFlowData = personnelService.getPassFlowData();
				if (passFlowData == null) {
					messageBean.setCode("nodata");
					messageBean.setMessage("车站客流趋势暂无数据");
					return messageBean;
				}
			}
			messageBean.setCode("success");
			messageBean.setMessage("车站客流趋势数据获取成功");
			messageBean.setData(passFlowData);
		} catch (Exception e) {
			messageBean.setCode("fail");
			messageBean.setMessage("车站客流趋势数据获取失败");
		}
		return messageBean;
	}

	/**
	 * 周边客流统计
	 * 
	 * @return
	 */
	@RequestMapping(value = "personnel/StatisticsData")
	@ResponseBody
	public MessageBean getPeripheralStatisticsData() {
		MessageBean messageBean = new MessageBean();
		List<Map<String, String>> statisticsData = new ArrayList<>();
		Map<String, Object> resultData = new HashMap<>();
		try {
			if ("test".equals(StartUpClass.StatisticsData)) {
				String time = "06:00,07:00,08:00,09:00,10:00,11:00,12:00";
				String NBLStatistics = "740,738,644,344,443,541,645"; // 诸光路
				String STHStatistics = "6393,6423,11435,4368,5345,4442,11347"; // 虹桥火车站
				String XJDStatistics = "600,543,535,638,735,542,447"; // 徐泾东
				resultData.put("time", time);
				resultData.put("NBLStatistics", NBLStatistics);
				resultData.put("STHStatistics", STHStatistics);
				resultData.put("XJDStatistics", XJDStatistics);
			} else {
				statisticsData = personnelService.getPeripheralStatisticsData();
				if (statisticsData == null || statisticsData.size() == 0) {
					messageBean.setCode("nodata");
					messageBean.setMessage("周边客流统计暂无数据");
					return messageBean;
				}
				String NBLStatistics = null; // 诸光路
				String STHStatistics = null; // 虹桥火车站
				String XJDStatistics = null; // 徐泾东
				String time = statisticsData.get(0).get("saveTime");
				for (Map<String, String> map : statisticsData) {
					String stationCN = map.get("stationCN");
					switch (stationCN) {
					case "NBL":
						NBLStatistics = map.get("klValue");
						break;
					case "STH":
						STHStatistics = map.get("klValue");
						break;
					case "XJD":
						XJDStatistics = map.get("klValue");
						break;
					}
				}

				resultData.put("time", time);
				resultData.put("NBLStatistics", NBLStatistics);
				resultData.put("STHStatistics", STHStatistics);
				resultData.put("XJDStatistics", XJDStatistics);
			}
			messageBean.setCode("success");
			messageBean.setMessage("周边客流统计数据获取成功");
			messageBean.setData(resultData);
		} catch (Exception e) {
			messageBean.setCode("fail");
			messageBean.setMessage("周边客流统计数据获取失败");
		}
		return messageBean;
	}

	/**
	 * 区域客流统计
	 * 
	 * @return
	 */
	/**
	 * @return
	 */
	@RequestMapping(value = "personnel/regionalData")
	@ResponseBody
	public MessageBean getRegionalPassFlowDensity() {
		MessageBean messageBean = new MessageBean();
		Map<String, Object> regionalData = new HashMap<>();
		try {
			if ("test".equals(StartUpClass.regionalData)) {
				int[] dataList = { 5, 10, 9, 15 };
				regionalData.put("dataList", dataList);
			} else {
				Jedis jedis = null;
				int[] resultArray = new int[] {10,10,20,20};
				try {
					jedis = JedisUtilProxy.getJedis();
					// redis中key:value 9237:售检票 9238:上行站台 9239:下行站台 9240:3号闸机口 9241:2号闸机口
					List<String> dataList = jedis.hmget("17DATA", "9238", "9239", "9240" , "9241");
					for(int i = 0; i < dataList.size(); i++) 
					{
						int upNum = 0;
						if(StringUtils.isNotBlank(dataList.get(i)))
						{
							upNum = Integer.parseInt(dataList.get(i));
						}
						resultArray[i] = (int)(((float) upNum / Integer.parseInt(StartUpClass.DHPassengerBase[i])) * 100); //从redis取出实时值与配置文件中的客流基准值进行百分比运算
					}
				} catch (JedisNotInitializeException e) {
					e.printStackTrace();
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}finally {
					JedisUtilProxy.close(jedis);
				}
				regionalData.put("dataList", resultArray);
			}
			messageBean.setCode("success");
			messageBean.setMessage("区域客流密度数据获取成功");
			messageBean.setData(regionalData);
		} catch (Exception e) {
			messageBean.setCode("fail");
			messageBean.setMessage("区域客流密度数据获取失败");
		}
		return messageBean;
	}

	/**
	 * 工时分析
	 * @return
	 */
	@RequestMapping(value = "personnel/hourAnalysis")
	@ResponseBody
	public MessageBean getHourAnalysisData() {
		MessageBean messageBean = new MessageBean();
		Map<String, Object> resultData = new HashMap<>();
		String[] categories = { "站务员3", "站务员2", "站务员1", "值班员", "站长" };
		try {
			List list2 = new LinkedList<>();
			if ("test".equals(StartUpClass.hourAnalysis)) {
//				for (int i = 0; i < categories.length; i++) {
//					List list1 = new LinkedList<>();
//					long baseTime = System.currentTimeMillis();
//					for (int j = 0; j < 5; j++) {
//						long duration = Math.round(Math.random() * 10000000);
//						long[] l = new long[3];
//						l[0] = i;
//						l[1] = baseTime;
//						l[2] = (baseTime += duration);
//						list1.add(l);
//						baseTime += Math.round(Math.random() * 1000000);
//					}
//					list2.add(list1);
//				}
				//获取每天8点时间戳
			    Calendar c = Calendar.getInstance();
			    c.set(Calendar.HOUR_OF_DAY, 8);
			    c.set(Calendar.MINUTE, 0);
			    c.set(Calendar.SECOND, 0);
			    c.set(Calendar.MILLISECOND, 0);
			    Long eight=c.getTimeInMillis();
				for (int i = 0; i < categories.length; i++) {
					List list1 = new LinkedList<>();
					long[] l = new long[3];
					long baseTime = System.currentTimeMillis();
					if(baseTime>eight) {
						l[0] = i;
						l[1] = eight;
						l[2] = baseTime;
						list1.add(l);
					}else {
						l[0] = i;
						l[1] = baseTime;
						l[2] = eight;
						list1.add(l);
					}
					list2.add(list1);
				}
				resultData.put("categories", categories);
				resultData.put("analysisData", list2);
				messageBean.setCode("success");
				messageBean.setMessage("工时分析数据获取成功");
				messageBean.setData(resultData);
			} else {
				Jedis jedis = null;
				try {
					jedis = JedisUtilProxy.getJedis();
					String str = jedis.hget("OTHER_DATA", "20");
					messageBean.setData(str);
					messageBean.setCode("success");
					messageBean.setMessage("工时分析数据获取成功");
				} catch (JedisNotInitializeException e) {
					e.printStackTrace();
				}finally {
					JedisUtilProxy.close(jedis);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			messageBean.setCode("fail");
			messageBean.setMessage("工时分析数据获取失败");
		}
		return messageBean;
	}

	/**
	 * 文件学习
	 * @return
	 */
	@RequestMapping(value = "personnel/fileStudy")
	@ResponseBody
	public MessageBean geFileStudyData() {
		MessageBean messageBean = new MessageBean();
		Map<String, Object> resultData = new HashMap<>();
		try {
			if ("test".equals(StartUpClass.fileStudy)) {
				resultData.put("fileTotal", 100);
				resultData.put("studyFile", 100);
			} else {
				Jedis jedis = null;
				try {
					jedis = JedisUtilProxy.getJedis();
				} catch (JedisNotInitializeException e) {
					e.printStackTrace();
				}
				int fileTotal =  Integer.parseInt(jedis.hmget("OTHER_DATA", "21").get(0));//文件学习总数
				int studyFile = fileTotal;
				JedisUtilProxy.close(jedis);
				resultData.put("fileTotal", fileTotal);
				resultData.put("studyFile", studyFile);
			}
			messageBean.setType("success");
			messageBean.setMessage("文件学习数据获取成功");
			messageBean.setData(resultData);
		} catch (Exception e) {
			e.printStackTrace();
			messageBean.setType("fail");
			messageBean.setMessage("文件学习数据获取失败");
		}
		return messageBean;
	}

	public String[] getHourArray() {
		// 获取当前时间的小时以及前6小时
		SimpleDateFormat df = new SimpleDateFormat("hh:00");
		String[] hourArray = new String[7];
		Calendar calendar = Calendar.getInstance();
		for (int i = 0; i < 7; i++) {
			if (i == 0) {
				calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
			} else {
				calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
			}
			String returnstr = df.format(calendar.getTime());
			hourArray[6 - i] = returnstr;
		}
		// Collections.reverse(Arrays.asList(hourArray));
		return hourArray;
	}
	
	public static void main(String[] args)
	{
		int i = (int)(((float)90/127) * 100);
		System.out.println(i);
	}

}
